<template>
  <el-card class="gray-card no-border no-margin-top" shadow="never">
    <el-form ref="form" :model="form" class="form">
      <el-row>
        <el-col :span="12">
          <el-form-item label="检验编号：">
            <el-input v-model="form.detectionNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属项目：">
            <el-select v-model="form.projectId"
                       @change="handleChangeProject"
                       placeholder="请选择所属项目">
              <el-option
                v-for="item in itemList"
                :key="item.id"
                :label="item.itemName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="仪器编号：">
            <el-select v-model="form.instrumentNo"
                       @change="handleInstrumentChange"
                       placeholder="请选择仪器">
              <el-option
                v-for="item in insturmentList"
                :key="item.instrumentNo"
                :label="item.instrumentName"
                :value="item.instrumentNo">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检验时间：">
            <el-date-picker
              v-model="form.detectionTime"
              type="datetime"
              placeholder="选择检验时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="所在科室：">
        <treeselect v-model="form.deptId"
                    :options="deptOptions"
                    @select="handleNodeClick"
                    :show-count="true"
                    placeholder="请选择所在科室"/>
      </el-form-item>

      <el-row>
        <el-col :span="12">
          <el-form-item label="患者姓名：">
            <el-input v-model="form.patientName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="患者性别：">
            <el-select v-model="form.gender" placeholder="请选择">
              <el-option
                v-for="item in sexList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="患者年龄：">
            <el-input-number
              v-model="form.age"
              :min="1"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="患者生日：">
            <el-date-picker
              v-model="form.birthday"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="病例号：">
            <el-input v-model="form.caseNumber"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="主治医生：">
            <el-input v-model="form.attendingDoctor"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="检验员：">
            <el-input v-model="form.inspector"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="样本编号：">
            <el-input v-model="form.specimenNo"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="采样时间：">
            <el-date-picker
              v-model="form.samplingTime"
              type="datetime"
              placeholder="选择采样时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检验结果：">
            <el-select v-model="form.result" placeholder="请选择检验结果">
              <el-option
                v-for="item in checkResultList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="检验结果值：">
            <el-input-number
              v-model="form.detectionValue"
              :min="0"
              :step="0.1"
              :precision="1"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="诊断结果：">
        <el-input v-model="form.diagnostic" type="textarea" class="textarea"></el-input>
      </el-form-item>

      <el-form-item label="医生建议：">
        <el-input v-model="form.doctorsAdvice" type="textarea" class="textarea"></el-input>
      </el-form-item>

    </el-form>
    <div class="details-foot">
      <div class="cell-btn">
        <el-button type="primary" @click.native="onSubmit">提交</el-button>
        <el-button @click.native="handleClose">取消</el-button>
      </div>
    </div>
  </el-card>
</template>

<script>
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {parseTime} from '@/utils/ruoyi'
import {selectList} from "@/api/business/project"
import {queryItemList} from "@/api/business/projectTestitems"
import {queryList} from "@/api/business/instrument"
import {updateDetectionResult, addDetectionResult} from "@/api/business/detectionResult"

export default {
  name: "addexamine",
  data() {
    return {
      checkResultList: [
        {
          label: '正常',
          value: '1'
        },
        {
          label: '异常',
          value: '0'
        }
      ],
      form: {
        projectId: '',
        projectName: '',
        deptId: 100,
        deptName: '湖南省人民医院',
        detectionNo: '',
        detectionTime: '',
        patientName: '',
        gender: '',
        age: '',
        birthday: '',
        caseNumber: '',
        attendingDoctor: '',
        doctorPost: '',
        detectionSample: '',
        specimenNo: '',
        instrumentName: '',
        instrumentNo: '',
        inspector: '',
        diagnostic: '',
        type: '',
        doctorsAdvice: '',
        result: '',
        reportTime: '',
        samplingTime: '',
        id: null,
        detectionValue: 0
      },
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      prjectList: [],
      insturmentList: [],
      sexList: [
        {
          label: '男',
          value: '2'
        },
        {
          label: '女',
          value: '3'
        }
      ],
      itemList: []
    };
  },
  components: {Treeselect},
  watch: {},
  created() {
    this.$nextTick(() => {
      this.getTreeselect();
      this.handleGetProjectList();
      this.handleGetInsturmentList();
      this.handleGetItemList();
    })
  },
  methods: {
    onSubmit() {
      this.form.birthday = parseTime(this.form.birthday)
      this.form.detectionTime = parseTime(this.form.detectionTime)
      this.form.samplingTime = parseTime(this.form.samplingTime)
      if (this.form.id) {
        updateDetectionResult(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      } else {
        addDetectionResult(this.form).then((res) => {
          this.$message.success('操作成功')
          this.handleClose()
        })
      }
    },
    // 获取检测项
    handleGetItemList() {
      queryItemList().then((res) => {
        this.itemList = res
      })
    },
    handleData(obj) {
      this.form = obj;
    },
    handleInstrumentChange() {
      this.form.instrumentName = this.insturmentList.filter(x => x.instrumentNo == this.form.instrumentNo)[0].instrumentName
    },
    handleNodeClick(data) {
      this.form.deptName = data.label
    },
    handleChangeProject() {
      this.form.projectName = this.prjectList.filter(x => x.id == this.form.projectId)[0].itemName
    },
    handleClose() {
      this.form = {}
      this.$emit('close', false)
    },
    // 获取仪器数据
    handleGetInsturmentList() {
      queryList().then((res) => {
        this.insturmentList = res
      })
    },
    // 获取项目
    handleGetProjectList() {
      selectList().then((res) => {
        if (res) {
          this.prjectList = res
        }
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
  }
};
</script>
